<template>
  <div class="app-container">
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column
        align="center"
        :label="$t('table.id')"
        width="80"
      >
        <template slot-scope="{row}">
          <span>{{ row.rowNumber }}</span>
        </template>
      </el-table-column>

      <el-table-column
        width="180px"
        align="center"
        :label="$t('table.date')"
      >
        <template slot-scope="{row}">
          <span>{{ row.createTime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>

      <el-table-column
        width="120px"
        align="center"
        :label="$t('table.author')"
      >
        <template slot-scope="{row}">
          <span>{{ row.createUser }}</span>
          <!-- <span>暂不显示</span> -->
        </template>
      </el-table-column>

      <el-table-column
        width="100px"
        :label="$t('table.importance')"
      >
        <!-- <span>暂不显示</span> -->
        <template slot-scope="{row}">
          <svg-icon
            v-for="n in +row.importance"
            :key="n"
            icon-class="star"
            class="meta-item__icon"
          />
        </template>
      </el-table-column>

      <el-table-column
        class-name="status-col"
        :label="$t('table.status')"
        width="110"
      >
        <template slot-scope="{row}">
          <el-tag :type="row.blogStatus | statusFilter">
            {{ row.blogStatus }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column
        min-width="300px"
        :label="$t('table.title')"
      >
        <template slot-scope="{row}">
          <router-link
            :to="'/blog/edit/'+row.blogId"
            class="link-type"
          >
            <span>{{ row.blogTitle }}</span>
          </router-link>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        :label="$t('table.actions')"
        width="120"
      >
        <template slot-scope="row">
          <router-link :to="'/blog/edit/'+row.blogId">
            <el-button
              type="primary"
              size="small"
              icon="el-icon-edit"
            >
              {{ $t('table.edit') }}
            </el-button>
          </router-link>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { fetchList, pullList } from '@/api/article'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'ArticleList',
  components: { Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20
      }
    }
  },
  created() {
    this.pullAllList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.items
        this.total = response.data.total
        this.listLoading = false
      })
    },
    pullAllList() {
      pullList(this.listQuery).then(response => {
        this.list = response.data.items
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
